﻿using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Localization;
using Utils.WidgetInserter.Models;

namespace Utils.WidgetInserter.Drivers {
    public class SettingsPartDriver : ContentPartDriver<SettingsPart> {
        private const string TemplateName = "Parts/Settings";

        public SettingsPartDriver() {
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        protected override string Prefix { get { return "Settings"; } }

        protected override DriverResult Editor(SettingsPart part, dynamic shapeHelper) {
            return ContentShape("Parts_WidgetInserterSettings_Edit",
                    () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix))
                    .OnGroup("Widget Inserter");
        }

        protected override DriverResult Editor(SettingsPart part, IUpdateModel updater, dynamic shapeHelper) {
            return ContentShape("Parts_WidgetInserterSettings_Edit", () =>
            {                    
                    updater.TryUpdateModel(part, Prefix, null, null);                    
                    return shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: part, Prefix: Prefix);
                })
                .OnGroup("Widget Inserter");
        }
    }
}